<?php
/**
 * User: aka
 * Date: 2018/6/25
 * Time: 14:50
 */

namespace app\api\controller;


use app\backstage\model\User;
use app\common\request\RequestOperation;

class Manage
{
    public $requestOperation;
    public $manageModel;
    public $userModel;

    public function __construct()
    {
        $this->requestOperation = new RequestOperation();
        $this->manageModel = new \app\backstage\model\Manage();
        $this->userModel = new User();
    }

    public function __destruct()
    {
        unset($this->requestOperation);
        unset($this->manageModel);
        unset($this->userModel);
    }

    private function getRequestOperation() : RequestOperation
    {
        return $this->requestOperation;
    }

    private function getManageModel() : \app\backstage\model\Manage
    {
        return $this->manageModel;
    }

    private function getUserModel() : User
    {
        return $this->userModel;
    }

    public function enterprisePhoneApi()
    {
        $requestData = $this->getRequestOperation()->getAllRequestParam();
        $this->getRequestOperation()->requestValidate('EnterprisePhone',$requestData);
        $result = $this->getManageModel()->enterprisePhoneAction($requestData);
        $result ?
            $this->getRequestOperation()->successCodeToJson(200)
            :
            $this->getRequestOperation()->errorCodeToJson(500);
        ;
    }

    public function contactAddressApi()
    {
        $requestData = $this->getRequestOperation()->getAllRequestParam();
        $this->getRequestOperation()->requestValidate('ContactAddress',$requestData);
        $result = $this->getManageModel()->contactAddressAction($requestData);
        $result ?
            $this->getRequestOperation()->successCodeToJson(200)
            :
            $this->getRequestOperation()->errorCodeToJson(500);
        ;
    }

    public function enterpriseCooperationApi()
    {
        $requestData = $this->getRequestOperation()->getAllRequestParam();
        $result = $this->getManageModel()->enterpriseCooperationAction($requestData);
        $result ?
            $this->getRequestOperation()->successCodeToJson(200)
            :
            $this->getRequestOperation()->errorCodeToJson(500);
        ;
    }

    public function enterpriseRecoveryApi()
    {
        $requestData = $this->getRequestOperation()->getAllRequestParam();
        $result = $this->getManageModel()->enterpriseRecoveryAction($requestData);
        $result ?
            $this->getRequestOperation()->successCodeToJson(200)
            :
            $this->getRequestOperation()->errorCodeToJson(500);
        ;
    }

    public function aboutUsApi()
    {
        $requestData = $this->getRequestOperation()->getAllRequestParam();
        $result = $this->getManageModel()->aboutUsAction($requestData);
        $result ?
            $this->getRequestOperation()->successCodeToJson(200)
            :
            $this->getRequestOperation()->errorCodeToJson(500);
        ;
    }

    public function processFlowApi()
    {
        $requestData = $this->getRequestOperation()->getAllRequestParam();
        $result = $this->getManageModel()->processFlowAction($requestData);
        $result ?
            $this->getRequestOperation()->successCodeToJson(200)
            :
            $this->getRequestOperation()->errorCodeToJson(500);
        ;
    }

    public function mattersNeedingAttentionApi()
    {
        $requestData = $this->getRequestOperation()->getAllRequestParam();
        $result = $this->getManageModel()->mattersNeedingAttentionAction($requestData);
        $result ?
            $this->getRequestOperation()->successCodeToJson(200)
            :
            $this->getRequestOperation()->errorCodeToJson(500);
        ;
    }

    public function exemptionFromDepositApi()
    {
        $requestData = $this->getRequestOperation()->getAllRequestParam();
        $result = $this->getManageModel()->exemptionFromDepositAction($requestData);
        $result ?
            $this->getRequestOperation()->successCodeToJson(200)
            :
            $this->getRequestOperation()->errorCodeToJson(500);
        ;
    }

    public function ercodeApi()
    {
        $file = request()->file('ercode_image');
        !empty($file)?:$this->getRequestOperation()->errorCodeToJson('请上传图片');
        if($file){
            $info = $file->move('./static/uploads/');
            if($info){
                $singleImgUrl =  '/static/uploads/'.$info->getSaveName();
                $result = $this->getManageModel()->ercodeUploadAction($singleImgUrl);
                $result ?
                    $this->getRequestOperation()->successCodeToJson(200)
                    :
                    $this->getRequestOperation()->errorCodeToJson(500);
                ;
            }else{
                echo $file->getError();
            }
        }
    }

    public function statusOperationApi()
    {
        $requestData = $this->getRequestOperation()->getAllRequestParam();
        $returnNum = $this->getManageModel()->statusOperationAction($requestData);
        $result = $returnNum == 0;
        $result ?
            $this->getRequestOperation()->successCodeToJson(['status'=>200,'id'=>$requestData['id']])
            :
            $this->getRequestOperation()->successCodeToJson(['status'=>201,'id'=>$requestData['id']]);
    }

    public function sowingImgAddApi()
    {
        $file = request()->file('sowing_img');
        !empty($file)?:$this->getRequestOperation()->errorCodeToJson('请上传图片');
        if($file){
            $info = $file->move('./static/uploads/');
            if($info){
                $singleImgUrl =  '/static/uploads/'.$info->getSaveName();
                $result = $this->getManageModel()->sowingImgUploadAction($singleImgUrl);
                $result ?
                    $this->getRequestOperation()->successCodeToJson(200)
                    :
                    $this->getRequestOperation()->errorCodeToJson(500);
                ;
            }else{
                echo $file->getError();
            }
        }
    }

    public function sowingImgEditApi()
    {
        $requestData = $this->getRequestOperation()->getAllRequestParam();
        $file = request()->file('sowing_edit_img');
        !empty($file)?:$this->getRequestOperation()->errorCodeToJson('请上传图片');
        if($file){
            $info = $file->move('./static/uploads/');
            if($info){
                $singleImgUrl =  '/static/uploads/'.$info->getSaveName();
                $result = $this->getManageModel()->sowingImgEditAction($requestData['id'],$singleImgUrl);
                $result ?
                    $this->getRequestOperation()->successCodeToJson(200)
                    :
                    $this->getRequestOperation()->errorCodeToJson(500);
                ;
            }else{
                echo $file->getError();
            }
        }
    }

    public function changeUserPasswordApi()
    {
        $requestData = $this->getRequestOperation()->getAllRequestParam();
        $this->getRequestOperation()->requestValidate('ChangePassword',$requestData);
        $result = $this->getUserModel()->changeUserPasswordAction($requestData);
        $result ?
            $this->getRequestOperation()->successCodeToJson(200)
            :
            $this->getRequestOperation()->errorCodeToJson(501);
        ;
    }

}